package com.bingxue.edu.common.wechat;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.bingxue.edu.common.config.WechatProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author panbo
 * @since 2024/9/15
 */
@Configuration
@RequiredArgsConstructor
public class WechatConfiguration {

    private final WechatProperties wechatProperties;

    @Bean(name = "parentWxMaService")
    public WxMaService parentWxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(wechatProperties.getParentAppId());
        config.setSecret(wechatProperties.getParentAppSecret());
        WxMaService maService = new WxMaServiceImpl();
        maService.setWxMaConfig(config);
        return maService;
    }

    @Bean(name = "employeeWxMaService")
    public WxMaService employeeWxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(wechatProperties.getEmployeeAppId());
        config.setSecret(wechatProperties.getEmployeeAppSecret());
        WxMaService maService = new WxMaServiceImpl();
        maService.setWxMaConfig(config);
        return maService;
    }

    @Bean
    public WxCpService wxCpService() {
        WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
        config.setCorpId(wechatProperties.getCorpId());
        config.setCorpSecret(wechatProperties.getCorpSecret());
        config.setAgentId(wechatProperties.getCorpAgentId());
        config.setToken(wechatProperties.getCorpToken());
        config.setAesKey(wechatProperties.getCorpAesKey());


        WxCpServiceImpl wxCpService = new WxCpServiceImpl();
        wxCpService.setWxCpConfigStorage(config);
        return wxCpService;
    }
}
